import {createRouter, createWebHistory} from "vue-router";
import Index from "../pages/Index.vue";
import Login from "../pages/Login.vue";
import UserList from "../pages/user/UserList.vue";
import Welcome from "../pages/Welcome.vue";

const routes = [
    {
        path: '/index', name: 'index', component: Index,
        children: [
            {path:'/index',name:'welcome',component: Welcome},
            {path:'userlist',name:'userlist',component:UserList}
        ]
    },
    {path: '/login', name: 'login', component: Login},
    {path: '/', redirect: 'index'},
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    console.log("前置守卫")
    let token = sessionStorage.getItem("token");
    if (to.name == 'login') {
        next()
    } else {
        if (!token) {
            next({
                name: 'login'
            })
        } else {
            next();
        }
    }
})

router.afterEach((to, from) => {
    console.log('后置守卫')
})

export default router